include YoghurtGum.mk
-include $(t)/Project.mk

#CRYSTAX=
#ifeq ($(YG_CRYSTAX),true)
#	CRYSTAX=-DYG_ANDROID_CRYSTAX
#endif
CRYSTAX=-DYG_ANDROID_CRYSTAX

include Build/find-source.mk
ifeq ($(YG_TYPE), static)
	include Build/static-lib.mk
else ifeq ($(YG_TYPE), shared)
	include Build/shared-lib.mk
else ifeq ($(YG_TYPE), game)
	include Build/game-project.mk
endif
include Build/create-project.mk

ifneq ($(v),)
	C=-v
else
	C=-q
endif

all: build-libraries game

archive: archive-compile

game: game-project game-compile-c++ game-compile-java

device_prefix:=
ifneq (unknown, $(shell adb -d get-state))
	device_prefix:=-d
endif
	
device-check:
ifneq (unknown, $(shell adb -d get-state))
	$(D)echo "Running on phone."
else
	$(D)echo "Running in emulator."
endif

run: deploy execute log

game-check:
ifeq ($(t),)
	$(error No project was set. Run make like so: "make t=Tests/Galaxians")
endif

stack-trace:
	$(D)/home/testbox/Android/android-ndk-r5/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/arm-linux-androideabi/bin/objdump -d $(YG_ROOT)/Tests/Galaxians/int/libs/armeabi/libGalaxians.so | grep $(s)
	@#$(D)/home/testbox/Android/android-ndk-r5/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/arm-linux-androideabi/bin/objdump -d $(t) | grep $(s)
	
	
generate-headers: game-check
	$(D)cd $(t)/android/bin/classes && javah -jni yg.engine.YGRenderer
	$(D)cd $(t)/android/bin/classes && javah -jni yg.engine.YGView
	$(D)cd $(t)/android/bin/classes && javah -jni yg.engine.YGActivity
	echo "Done."
	
test1:
	@echo "Msg: $(msg)"

test2:
	@echo "Sup."
	$(call test1)

# ==================================
# Running
# ==================================

emulator:
	$(D)$(YG_SDK_TOOLS)/emulator -avd $(YG_VIRTUAL_DEVICE) -shell

deploy: device-check
	@echo "*** DEPLOYING ***"
	$(D)$(YG_SDK_PLATFORM_TOOLS)/adb $(device_prefix) uninstall $(PROJECT_PACKAGE_NAME)
	$(D)$(YG_SDK_PLATFORM_TOOLS)/adb $(device_prefix) install -r $(t)/bin/$(PROJECT_NAME).apk
	
execute:
	@echo "*** EXECUTING ***"
	$(D)$(YG_SDK_PLATFORM_TOOLS)/adb $(device_prefix) logcat -c
	$(D)$(YG_SDK_PLATFORM_TOOLS)/adb $(device_prefix) shell am start -n $(PROJECT_PACKAGE_NAME)/.$(PROJECT_NAME)
	
log:
	@echo "*** LOGGING ***"
	$(D)$(YG_SDK_PLATFORM_TOOLS)/adb $(device_prefix) shell logcat

# ==================================
# Game
# ==================================

clean-game: game-check project-clean

build-game: game-check
	$(D)for i in $(YG_SOURCE_DIRS) ; do \
		make t=$(t) dir=$$i project-compile ; \
	done
	$(D)make t=$(t) project-link
	
rebuild-game: game-check project-clean build-game
	
# ==================================
# Archive
# ==================================

clean-library: game-check
ifeq ($(YG_TYPE), shared)
	$(D)make t=$(t) shared-clean
else ifeq ($(YG_TYPE), static)
	$(D)make t=$(t) static-clean
endif

build-library: game-check
ifeq ($(YG_TYPE), shared)
	$(D)for i in $(YG_SOURCE_DIRS) ; do \
		make t=$(t) dir=$$i shared-compile ; \
	done
	$(D)make t=$(t) shared-link
else ifeq ($(YG_TYPE), static)
	$(D)for i in $(YG_SOURCE_DIRS) ; do \
		make t=$(t) dir=$$i static-compile ; \
	done
	$(D)make t=$(t) static-link
endif

rebuild-library: game-check
ifeq ($(YG_TYPE), shared)
	$(D)make t=$(t) shared-clean
	$(D)for i in $(YG_SOURCE_DIRS) ; do \
		make t=$(t) dir=$$i shared-compile ; \
	done
	$(D)make t=$(t) shared-link
else ifeq ($(YG_TYPE), static)
	$(D)make t=$(t) static-clean
	$(D)for i in $(YG_SOURCE_DIRS) ; do \
		make t=$(t) dir=$$i static-compile ; \
	done
	$(D)make t=$(t) static-link
endif

# ==================================
# Third-Party
# ==================================

build-third-party:
	$(D)make t=Third-Party/TinyImageLoader build-library
	$(D)make t=Third-Party/libzip build-library

clean-third-party:
	$(D)make t=Third-Party/TinyImageLoader clean-library build-library
	$(D)make t=Third-Party/libzip clean-library
	
rebuild-third-party:
	$(D)make t=Third-Party/TinyImageLoader clean-library build-library
	$(D)make t=Third-Party/libzip clean-library build-library

# ==================================
# Libraries
# ==================================

build-libraries:
	$(D)make t=Source/YoghurtGumBase build-library
	$(D)make t=Source/YoghurtGumMain build-library
	$(D)make t=Modules/RenderMethodOpenGLES build-library

clean-libraries:
	$(D)make t=Source/YoghurtGumBase clean-library
	$(D)make t=Source/YoghurtGumMain clean-library
	$(D)make t=Modules/RenderMethodOpenGLES clean-library
	
rebuild-libraries:
	$(D)make t=Source/YoghurtGumBase clean-library build-library
	$(D)make t=Source/YoghurtGumMain clean-library build-library
	$(D)make t=Modules/RenderMethodOpenGLES clean-library build-library

# ==================================
# Game
# ==================================	

game-test: game-check project-build

old-game-clean-java: game-check
	@echo "###    Cleaning Java   ###"
	$(D)rm -f $(t)/$(APP_BIN)/*.apk

game-project: game-check
	@# Create the project files if they do not exist
ifeq (exists, $(shell [ -f $(t)/$(APP_INT)/build.xml ] && echo exists)) 
	@echo "###  Updating Game Project  ###"
	$(D)$(YG_SDK_TOOLS)/android update project \
	--target $(YG_TARGET_VERSION) \
	--path $(t)/$(APP_INT) \
	--name $(APP_NAME)
else
	@echo "### Generating Game Project ###"
	$(D)$(YG_SDK_TOOLS)/android create project \
	--target $(YG_TARGET_VERSION) \
	--path $(t)/$(APP_INT) \
	--name $(APP_NAME) \
	--activity $(APP_NAME) \
	--package $(APP_PACKAGE_NAME)
endif

	@echo "\nkey.store=$(YG_PASSFILE)"                 >> "$(t)/$(APP_INT)/build.properties"
	@echo "key.alias=$(YG_PASSFILE_ALIAS)"             >> "$(t)/$(APP_INT)/build.properties"
	@echo "key.store.password=$(YG_PASSFILE_PW)"       >> "$(t)/$(APP_INT)/build.properties"
	@echo "key.alias.password=$(YG_PASSFILE_ALIAS_PW)" >> "$(t)/$(APP_INT)/build.properties"

old-game-compile-c++: game-check
	@echo "###  Compiling C++ Client   ###"
	$(D)(cd $(t); make build-c++)
	$(D)$(YG_STRIP) --strip-debug $(t)/$(APP_INT)/libs/armeabi/lib$(APP_NAME).so

old-game-compile-java: game-check
	@echo "###   Compiling Java Host   ###"
	$(D)cd $(t)/$(APP_INT) && ant $(C) release
